package com.tumanex.adapters;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tumanex.R;
import com.tumanex.database.DBCategory;

public class MyPicturesGalleryAdapter extends BaseAdapter {

	private static int count;

	private Context mContext;

	private ImageView myPicture;

	private TextView myPictureCount;

	private TextView myPictureName;

	private ArrayList<Map<String, Object>> images;
	private Map<String, Object> imageData;

	private DBCategory mDBCategory;

	public MyPicturesGalleryAdapter(Context _context) {

		mContext = _context;

		mDBCategory = new DBCategory(mContext);
		updataData();
	}

	public void updataData() {

		mDBCategory.open();

		getSaveImages();
		count = getCount();

		mDBCategory.close();
	}

	public void getSaveImages() {

		mDBCategory.fillArraysObjectSaveImage();
		images = mDBCategory.getObjectTableSave();
	}

	public Bitmap getImageByPos(int position) {

		imageData = images.get(position);

		byte[] image = (byte[]) imageData.get(DBCategory.ATTRIBUTE_IMG);

		Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
		Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);

		return mutableBitmap;
	}

	@Override
	public int getCount() {

		return images.size();
	}

	public void setImageViewParams(ImageView im) {

		myPicture.setPadding(15, 5, 15, 5);
		myPicture.setScaleType(ImageView.ScaleType.CENTER_CROP);
	}

	public void setTextViewParams(TextView tv) {

		tv.setTextSize(mContext.getResources().getDimension(
				R.dimen.image_name_size));
		tv.setTextColor(mContext.getResources().getColor(R.color.powderblue));
	}

	public void deleteSaveImage(int position) {

		mDBCategory.open();

		imageData = images.get(position);

		int id = (Integer) imageData.get(DBCategory.ATTRIBUTE_ID);

		mDBCategory.delRecSaveImage(id);

		mDBCategory.close();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View gallery;

		getImageByPos(position);

		if (convertView == null) {

			gallery = new View(mContext);
			gallery = inflater.inflate(R.layout.my_picture, null);

			myPictureName = (TextView) gallery
					.findViewById(R.id.mypicture_name);

			myPictureCount = (TextView) gallery
					.findViewById(R.id.mypicture_count);

			setTextViewParams(myPictureName);
			setTextViewParams(myPictureCount);

			myPictureCount.setText((position + 1) + " / " + count + "   ");

			String name = (String) imageData
					.get(DBCategory.ATTRIBUTE_NAME_IMAGE);
			myPictureName.setText(name);

			myPicture = (ImageView) gallery.findViewById(R.id.mypicture);

			setImageViewParams(myPicture);
			myPicture.setImageBitmap(getImageByPos(position));

		}

		else {

			gallery = (View) convertView;
		}

		return gallery;
	}

}
